<script>
  export default {
    name: 'LoginMobile',
    options: {
      styleIsolation: 'shared',
    },
  }
</script>
<!-- pages/login/components/mobile.vue -->

<script setup>
  import { useUserStore } from '@/stores/user.js'
  import { useRefreshTokenStore } from '@/stores/refreshToken.js'
  // 导入 verifyCodeApi 函数
  import { verifyCodeApi, loginApi } from '@/services/user.js'
  // 导入 ref 函数
  import { ref } from 'vue'
  // 声明一个变量 isShowCountdown，并初始化为 false
  const isShowCountdown = ref(false)
  const getVcode = async () => {
    //校验手机号
    await formRef.value.validateField(['mobile'])
    //显示倒计时
    isShowCountdown.value = true
    //发请求
    const res = await verifyCodeApi({
      // 获取表单中的手机号
      mobile: form.value.data.mobile,
      // 验证类型
      type: 'login',
    }).catch((err) => {
      // 验证失败，将倒计时关闭
      isShowCountdown.value = false
      // 返回错误信息
      return Promise.reject(err)
    })
    console.log(res)
    uni.utils.toast('已发送,请查收')
    form.value.data.code = res.data.code
  }

  const formRef = ref(null)
  const form = ref({
    data: {
      mobile: '13230000077',
      code: '',
    },
    rules: {
      mobile: {
        rules: [
          { required: true, errorMessage: '请填写手机号码' },
          { pattern: /^1[3-9]\d{9}$/, errorMessage: '手机号码格式不正确' },
        ],
      },
      code: {
        rules: [
          { required: true, errorMessage: '请输入验证码' },
          { pattern: /^\d{6}$/, errorMessage: '验证码格式不正确' },
        ],
      },
    },
  })

  const isAgree = ref(false)
  const userStore = useUserStore()
  const RefreshTokenStore = useRefreshTokenStore()
  const login = async () => {
    await formRef.value.validate()
    if (isAgree.value === false) {
      return uni.utils.toast('请阅读并同意用户协议')
    }
    const res = await loginApi(form.value.data)
    console.log(res.data)
    userStore.setBasicInfo = res.data
    RefreshTokenStore.refreshToken = res.data.refreshToken
    uni.utils.toast('登录成功')
    // uni.switchTab({ url: '/pages/index/index' })
    // 请求跳转方式
    uni[userStore.returnType]({
      // 请求跳转地址
      url: userStore.returnUrl,
    })
    //重置返回url和类型
    userStore.resetReturnUrlAndType()
  }
</script>

<template>
  <uni-forms
    class="login-form"
    ref="formRef"
    :rules="form.rules"
    :model="form.data"
  >
    <uni-forms-item name="mobile">
      <uni-easyinput
        :input-border="false"
        :clearable="false"
        placeholder="请输入手机号"
        v-model="form.data.mobile"
        placeholder-style="color: #C3C3C5"
      />
    </uni-forms-item>
    <uni-forms-item name="code">
      <uni-easyinput
        v-model="form.data.code"
        :input-border="false"
        :clearable="false"
        placeholder="请输入验证码"
        placeholder-style="color: #C3C3C5"
      />
      <view v-if="isShowCountdown" style="display: flex; color: #16c2a3">
        <!--   <xhx-countdown
          :second="59"
          :showDay="false"
          :showHours="false"
          :showMinutes="false"
          color="#16C2A3"
          @timeup="isShowCountdown = false"
        ></xhx-countdown>
        秒后再次获取 -->
        <xhx2-countdown
          :seconds="59"
          @timeup="isShowCountdown = false"
        ></xhx2-countdown>
        秒后再次获取
      </view>
      <text class="text-button" v-else @click="getVcode">获取验证码</text>
    </uni-forms-item>

    <view class="agreement">
      <radio :checked="isAgree" color="#16C2A3" @click="isAgree = !isAgree" />
      我已同意
      <text class="link">用户协议</text>
      及
      <text class="link">隐私协议</text>
    </view>

    <button class="uni-button" @click="login">登 录</button>
  </uni-forms>
</template>

//

<style lang="scss">
  @import './styles.scss';
</style>
